package com.superboboge;

import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserDAO userDAO;

    @Resource
    CbTestDAO cbTestDAO;


    @PostMapping(value = "/Users")
    public Integer addUser(User user){
        user.setGmtCreated(new Date());
        user.setGmtModified(new Date());
        return userDAO.save(user).getId();
    }

    @PutMapping(value = "/Users/{id}")
    public boolean updateUser(@PathVariable Integer id, String name, Integer age){
        try {
            User user = new User();
            user.setId(id);
            user.setAge(age);
            user.setName(name);
            user.setGmtModified(new Date());
            userDAO.save(user);
            return true;
        } catch (Exception e) {
            return  false;
        }
    }

    @GetMapping(value = "/Users/{id}")
    public User getUser(@PathVariable Integer id){
        return userDAO.findOne(id);
    }

    @GetMapping(value = "/Users")
    public Page<User> getUsers(User user,Integer page){
        Sort sort = new Sort(Sort.Direction.DESC, "gmtCreated");
        Pageable pageable = new PageRequest(page, 3, sort);
        return userDAO.findAll(Example.of(user),pageable);
    }
    @GetMapping(value = "/CbTest")
    public Page<CbTest> getCbTest(CbTest cbTest,Integer page){
        Pageable pageable = new PageRequest(page, 2);
        return cbTestDAO.findAll(Example.of(cbTest),pageable);
    }


    @GetMapping(value = "/Users1")
    public List<User> getUsers1(final User user, @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")final Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")final Date endDate) throws ParseException {
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicate = new ArrayList<>();
                if (null != startDate ){
                    predicate.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("gmtCreated"),startDate));
                }
                if(null != endDate){
                    predicate.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("gmtCreated"),endDate));
                }
                if (null != user.getName()){
                    predicate.add(criteriaBuilder.equal(root.get("name"),user.getName()));
                }

                return criteriaBuilder.and(predicate.toArray(new Predicate[predicate.size()]));
            }
        };

        return userDAO.findAll(specification);
    }

    @GetMapping(value = "/Users2")
    public List<User> getUsers2(UserParam userParam) throws ParseException {
        Specification<User> specification = com.github.wenhao.jpa.Specifications.<User>and()
                .ge(null!= userParam.getStartDate(),"gmtCreated",userParam.getStartDate())
                .build();

        return userDAO.findAll(specification);
    }

    @DeleteMapping(value = "/Users/{id}")
    public boolean deleteUser(@PathVariable  Integer id){
        try {
            userDAO.delete(id);
            return true;
        } catch (Exception e) {
            return  false;
        }
    }

}
